package com.spvs.job.task;


import cn.hutool.core.date.DateUtil;
import com.spvs.common.core.utils.DateUtils;
import com.spvs.job.domain.*;
import com.spvs.job.mapper.EquipPointPlanRecordMapper;
import com.spvs.job.mapper.EquipPointRecordDetailMapper;
import com.spvs.job.mapper.EquipPointRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 点检记录
 *
 * @author supervision
 */
@Component
@Slf4j
public class PointRecord {

    @Autowired
    private EquipPointPlanRecordMapper pointPlanRecordMapper;

    @Autowired
    private EquipPointRecordMapper pointRecordMapper;

    @Autowired
    private EquipPointRecordDetailMapper equipPointRecordDetailMapper;


    public void executeTasks() {
        log.info("点检计划任务。。。。");
        List<EquipPointPlanRecord> equipPointPlanRecords = pointPlanRecordMapper.
                selectAll(CommonEnum.YOU_XIAO.getCode());
        if (CollectionUtils.isEmpty(equipPointPlanRecords)) {
            log.info("未查询到点检计划。。。。");
            return;
        }
        for (EquipPointPlanRecord equipPointPlanRecord : equipPointPlanRecords) {
            Long planRecordId = equipPointPlanRecord.getId();
            UpkeepEnum byFormCode = UpkeepEnum.getByFormCode(equipPointPlanRecord.getCycle());
            List<EquipPointRecordDetail> equipPointRecordDetails =
                    equipPointRecordDetailMapper.selectEquipPointRecordDetailByPlanId(planRecordId);
            if (CollectionUtils.isEmpty(equipPointRecordDetails)) {
                log.info("{}未查询到绑定设备详情信息。。。", equipPointPlanRecord.getName());
                continue;
            }
            labe:
            for (EquipPointRecordDetail recordDetail : equipPointRecordDetails) {

                EquipPointRecord equipPointRecord = pointRecordMapper.selectEquipPointRecordByCode(recordDetail.getDeviceId());

                if (equipPointRecord != null) {
                    String days = DateUtils.dayDiff(equipPointRecord.getPlanPatrolTime(), new Date());
                    if (Integer.parseInt(days) < Integer.parseInt(byFormCode.getCode())) continue labe;
                    extracted(planRecordId, recordDetail);
                } else {
                    extracted(planRecordId, recordDetail);
                }
            }
        }
    }

    private void extracted(Long planRecordId, EquipPointRecordDetail recordDetail) {
        EquipPointRecord upkeepRecord = new EquipPointRecord();
        upkeepRecord.setPlanId(planRecordId);
        upkeepRecord.setDeviceId(recordDetail.getDeviceId());
        upkeepRecord.setEquipmentCode(recordDetail.getEquipmentCode());
        upkeepRecord.setEquipmentName(recordDetail.getEquipmentName());
        upkeepRecord.setPlanPatrolTime(DateUtil.beginOfDay(new Date()));
        pointRecordMapper.insertEquipPointRecord(upkeepRecord);
    }
}


